#include"code.h"
#include<string.h>
#include<unistd.h>

#define LENGTH 101
#define LABLE '#'


void FlushCode(double target, double current)
{
    //step  0
    const static char *sym = "|/-\\";
    static int index = 0;
    int len = strlen(sym);
    
    // step   1
    double rate = (current / target)*100.0;

    // step  2
    int cnt = (int)rate;

    // step 3
    char bar[LENGTH];
    memset(bar, 0, sizeof(bar));
    for(int i = 0; i < cnt; i++)
    {
        bar[i] = LABLE;
    }
    printf("[%-100s][%.1f%%][%c]\r ", bar, rate, sym[index++]);
    index %= len;
    fflush(stdout);

    if(rate >= 100.0)
    {
        printf("\n");
    }
}



void code()
{
    const char *symb = "|/-\\";
    int s_len = strlen(symb);
    char bar[LENGTH];
    memset(bar, '\0', sizeof(bar));
    int cnt = 0;
    while(cnt <= 100)
    {
        printf("[%-100s][%d%%][%c]\r", bar, cnt, symb[cnt % s_len]);
        fflush(stdout); 
        bar[cnt++] = LABLE;
        usleep(10000);
    }
    printf("\n");
}

